<template>
    <div style="padding:0 17px">
        <Breadcrumb/>
        <div style="display: flex;margin-top: 15px;">
            <el-button type="primary" style="margin-right: 15px" @click="addItem">新增
            </el-button>
            <span style="min-width: 150px;margin-right: 15px">
        <OrganizationTree
                @change="load"
                @tree-select-changed="treeSelectChanged"
                v-model="selectedOrganizationId"
        />
      </span>
            <el-input v-model="keyWord" placeholder="单位名称"
                      style="width: 300px;height: 33px;margin-right: 15px;font-size: 13px;" clearable></el-input>
            <el-button icon="Search" type="primary" style="color: #fff;font-size: 12px;background-color: #1890ff;" @click="load">搜索
            </el-button>
            <el-button type="success" style="" @click="importData">导入</el-button>
            <el-button type="success" style="" @click="exportData">导出</el-button>
        </div>

        <el-table :data="tableData" :row-style="{ height: '77px' }" border
                  style="width: calc(100vw - 240px);margin-top: 20px;height: 70vh;color: #909399;font-size: 15px;">
            <el-table-column prop="id" label="ID" width="100"/>
            <el-table-column prop="clientName" label="单位名称" width="200"/>
            <el-table-column prop="establishTime" label="成立时间" width="200"/>
            <el-table-column prop="telephone" label="电话" width="150"/>
            <el-table-column prop="faxes" label="传真" width="150"/>
            <el-table-column prop="clientPostcode" label="单位邮编" width="150"/>
            <el-table-column prop="email" label="e-mail" width="150"/>
            <el-table-column prop="functionary" label="法人代表或主要负责人" width="150"/>
            <el-table-column prop="functionaryTel" label="法人代表或主要负责人联系电话" width="150"/>
            <el-table-column prop="localPoliceStation" label="所属地区派出所" width="150"/>
            <el-table-column prop="superCompetClient" label="上级主管单位" width="200"/>
            <el-table-column prop="jurisdictClient" label="管辖单位" width="200"/>
            <el-table-column prop="fixedAssets" label="固定资产额(万元)" width="150"/>
            <el-table-column prop="maxWorkerNum" label="营业时最大人数(人)" width="150"/>
            <el-table-column prop="totalArea" label="占地总面积(m²)" width="150"/>
            <el-table-column prop="buildingArea" label="总建筑面积(m²)" width="150"/>
            <el-table-column prop="workerNum" label="职工人数" width="150"/>
            <el-table-column prop="econOwnership" label="经济所有制" width="150"/>
            <el-table-column prop="gasType" label="燃气类型" width="150"/>
            <el-table-column align="center" label="操作" fixed="right" width="180">
                <template v-slot="scope">
                    <el-button type="primary" size="small" icon="Edit" @click="editItem(scope.row)">编辑</el-button>
                    <el-button v-if="this.userInformation.role === 1" type="danger" size="small" icon="Delete" @click="deleteItem(scope.row.id)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                :current-page="currentPage"
                :page-size="pageSize"
                :page-sizes="[10, 20, 30, 50]"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                :background="true"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
        />
        <el-dialog title="消防安全基本情况操作" v-model="operationItemDialog" :close-on-click-modal="false" :show-close="false"
                   width="600">
            <el-form label-position="top" :inline="true" :model="form" ref="form" :rules="rules">
                <el-form-item style="width: 185px" label="单位名称" prop="clientId">
                    <el-select v-model="form.clientId" filterable placeholder="选择单位名称">
                        <el-option
                                v-for="item in clientOptions"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item style="width: 185px" label="成立时间" prop="establishTime">
                    <el-date-picker
                            v-model="form.establishTime"
                            type="date"
                            value-format="YYYY-MM-DD HH:mm:ss"
                            placeholder="选择成立时间"
                    />
                </el-form-item>
                <el-form-item label="电话" prop="telephone">
                    <el-input placeholder="电话" v-model="form.telephone"
                              autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="传真" prop="faxes">
                    <el-input placeholder="传真" v-model="form.faxes"
                              autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="单位邮编" prop="clientPostcode">
                    <el-input placeholder="单位邮编" v-model="form.clientPostcode"
                              autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="e-mail" prop="email">
                    <el-input placeholder="e-mail" v-model="form.email"
                              autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="法人代表或主要负责人" prop="functionary">
                    <el-input placeholder="法人代表或主要负责人" v-model="form.functionary"
                              autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="法人代表或主要负责人电话" prop="functionaryTel">
                    <el-input placeholder="法人代表或主要负责人联系电话" v-model="form.functionaryTel"
                              autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label-width="110" label="所属地区派出所" prop="localPoliceStation">
                    <el-input placeholder="所属地区派出所" v-model="form.localPoliceStation"
                              autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="上级主管单位" prop="superCompetClient">
                    <el-input placeholder="上级主管单位" v-model="form.superCompetClient"
                              autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="管辖单位" prop="jurisdictClient">
                    <el-input placeholder="管辖单位" v-model="form.jurisdictClient"
                              autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item label="固定资产额(万元)" prop="fixedAssets">
                    <el-input placeholder="固定资产额(万元)" v-model="form.fixedAssets"
                              autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="营业时最大人数(人)" prop="maxWorkerNum">
                    <el-input placeholder="营业时最大人数(人)" v-model="form.maxWorkerNum"
                              autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label-width="110" label="占地总面积(m²)" prop="totalArea">
                    <el-input placeholder="占地总面积(m²)" v-model="form.totalArea"
                              autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="总建筑面积(m²)" prop="buildingArea">
                    <el-input placeholder="总建筑面积(m²)" v-model="form.buildingArea"
                              autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="职工人数" prop="workerNum">
                    <el-input placeholder="职工人数" v-model="form.workerNum"
                              autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="经济所有制" prop="econOwnership">
                    <el-input placeholder="经济所有制" v-model="form.econOwnership"
                              autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="燃气类型" prop="gasType">
                    <el-input placeholder="燃气类型" v-model="form.gasType"
                              autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="reset('form')">取 消</el-button>
                <el-button type="primary" @click="submitForm('form')">确 定</el-button>
            </div>
        </el-dialog>
        <el-dialog title="导入" v-model="dialogImportVisible" width="520px">
            <el-form label-position="right" label-width="100px">
                <el-form-item label="模板下载:" padding-top:10px>
                    <el-button type="primary" size="small" style="" @click="exportTemplate">消防安全基本情况</el-button>
                </el-form-item>
                <el-form-item label="文件" padding-top:-20px>
                    <el-upload :headers="headers"
                           :multiple="false"
                               :on-success="onUploadSuccess"
                               :on-error="onUploadError"
                               :action="serverIp+'/fire-basic-infor/importData'"
                               class="upload-demo">
                        <el-button size="small" type="primary">点击上传</el-button>
                        <div slot="tip" class="el-upload__tip">只能上传xlsx文件</div>
                    </el-upload>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogImportVisible = false">取消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import request from "@/utils/request.js";
    import Breadcrumb from "@/components/Breadcrumb.vue";
    import OrganizationTree from "@/components/OrganizationTree.vue";
    import {serverIp} from "../../../public/static/config"

    export default {
        name: "FireBasicInfor",
        components: {Breadcrumb,OrganizationTree},
        data() {
            return {
                serverIp,
                headers: {
                    token:  JSON.parse(localStorage.getItem('user')).token,
                },
                dialogImportVisible: false,
                userInformation: JSON.parse(localStorage.getItem("user")),
                selectedOrganizationId: JSON.parse(localStorage.getItem("user")).organization,
                keyWord: "",
                currentData: {},
                tableData: [],
                clientOptions: [],
                searchClient: "",
                currentPage: 1,
                pageSize: 10,
                total: 0,
                operationItemDialog: false,  // 弹窗
                form: {},
                rules: {
                    clientId: [
                        {required: true, message: '请选择单位名称', trigger: 'blur'},
                    ],
                },
                isEdit: false,
            }
        },
        mounted() {
            // 初始化加载用户数据
            this.load()
            this.getClientOptions()
        },
        methods: {
            importData() {
                this.dialogImportVisible = true;
            },
            onUploadSuccess() {
                this.$message.success('上传成功');
                this.dialogImportVisible = false;
                location.reload();
            },
            onUploadError(){
                this.$message.error('上传失败');
            },
            // 模板下载
            exportTemplate() {
                window.location.href = '../xlsx/fireBasicInfor.xlsx'
            },
            exportData() {
                let level = JSON.parse(localStorage.getItem("user")).level
                if (this.currentData.level !== undefined) {
                    level = this.currentData.level
                }
                let isAllClient = JSON.parse(localStorage.getItem("user")).isAllClient;
                let clientIds;
                if (isAllClient === "1") {
                    clientIds = JSON.parse(localStorage.getItem("user")).clientIds;
                } else {
                    clientIds = [];
                }
                // if (this.searchClient === undefined) {
                //     this.searchClient = "";
                // }
                window.open(serverIp + '/fire-basic-infor/export?keyWord=' + this.keyWord +
                    "&level=" + level + "&organizationId=" + this.selectedOrganizationId + "&isAllClient=" + isAllClient + "&clientIds=" + clientIds);
            },
            getClientOptions() {
                let level = JSON.parse(localStorage.getItem("user")).level
                let organizationId = JSON.parse(localStorage.getItem("user")).organization
                let isAllClient = JSON.parse(localStorage.getItem("user")).isAllClient;
                let clientIds;
                if (isAllClient === "1") {
                    clientIds = JSON.parse(localStorage.getItem("user")).clientIds;
                } else {
                    clientIds = [];
                }
                request.get("/client/listbyName?name=" + this.searchClient + "&organizationId=" + organizationId + "&level=" + level + "&isAllClient=" + isAllClient + "&clientIds=" + clientIds).then(res => {
                    this.clientOptions = res.data
                })
            },
            // 搜索
            load() {
                let level = JSON.parse(localStorage.getItem("user")).level
                if (this.currentData.level !== undefined) {
                    level = this.currentData.level
                }
                let isAllClient = JSON.parse(localStorage.getItem("user")).isAllClient;
                let clientIds;
                if (isAllClient === "1") {
                    clientIds = JSON.parse(localStorage.getItem("user")).clientIds;
                } else {
                    clientIds = [];
                }
                // if (this.searchClient === "undefined") {
                //     this.searchClient = "";
                // }
                request.get("/fire-basic-infor/page?pageNum=" + this.currentPage + "&pageSize=" + this.pageSize + "&keyWord=" + this.keyWord +
                    "&level=" + level + "&organizationId=" + this.selectedOrganizationId + "&isAllClient=" + isAllClient + "&clientIds=" + clientIds).then(res => {
                    if (res.code === '200') {
                        for (let key in res.data) {
                            if (key === "record") {
                                this.tableData = res.data[key];
                            } else if (key === "total") {
                                this.total = res.data[key];
                            }
                        }
                    } else {
                        this.$message.error("错误" + res.msg)
                    }
                })
            },
            // 新增
            addItem() {
                this.operationItemDialog = true
            },
            // 编辑
            editItem(scope) {
                this.isEdit = true;
                Object.assign(this.form, scope)
                this.operationItemDialog = true
            },
            // 编辑、新增确定按钮
            submitForm(val) {
                this.form.operatorId = JSON.parse(localStorage.getItem("user")).id
                this.$refs[val].validate((valid) => {
                    if (valid) {
                        request.post("/fire-basic-infor/edit", this.form).then(res => {
                            if (res.code === '200') {
                                this.$message({
                                    message: "成功",
                                    type: "success"
                                })
                                this.load()
                                if (!this.isEdit) {
                                    this.clearConditions()
                                }
                                this.reset(val)
                            } else {
                                this.$message.error("操作失败" + res.msg)
                            }
                        })
                    }
                })
            },
            // 清空查询条件
            clearConditions() {
                this.selectedOrganizationId = JSON.parse(localStorage.getItem("user")).organization;
                this.keyWord = "";
            },
            reset(val) {
                this.isEdit = false;
                this.operationItemDialog = false;
                this.form = {};
                this.$refs[val].clearValidate();
            },
            // 删除确定弹窗
            deleteItem(scope) {
                this.$confirm('是否确定删除?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    request.delete("/fire-basic-infor/delete/" + scope).then(res => {
                        if (res.code === '200') {
                            this.$message.success("成功")
                            this.load()
                        } else {
                            this.$message.error(res.msg)
                        }
                    })
                });
            },
            treeSelectChanged(data) {
                this.currentData = data
            },
            handleSizeChange(pageSize) {   // 改变当前每页的个数触发
                this.pageSize = pageSize
                this.load()
            },
            handleCurrentChange(pageNum) {  // 改变当前页码触发
                this.currentPage = pageNum
                this.load()
            },
        }
    }
</script>

<style scoped>
    :deep(.el-table th .cell) {
        font-weight: 700
    }

    :deep(.el-select-dropdown__item) {
        font-size: 14px;
    }

    :deep(.el-select__wrapper) {
        font-size: 14px;
    }
</style>
